﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebSysCore;
using HSchSchedule.Utils;

namespace HSchSchedule.BackendPage
{
    public partial class LoginPage1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoginFromCookies();
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            Login();
        }
        private void Login()
        {
            string userName = txtUserName.Text;
            string password = txtPassword.Text;
            List<Customer> lstAllCustomer = DataBase.GetAllCustomer();
            bool ok = false;
            foreach (Customer c in lstAllCustomer)
            {

                if (c.LoginName.CompareTo(userName) == 0 && c.Password.CompareTo(password) == 0)
                {
                    ok = true;
                    if (chkRemember.Checked)
                    {
                        HttpCookie loginCookies = new HttpCookie(GUIConstants.LOGIN_COOKIES);
                        loginCookies[GUIConstants.LOGIN_COOKIES_USERNAME] = c.LoginName;
                        loginCookies[GUIConstants.LOGIN_COOKIES_PASSWORD] = c.Password;
                        loginCookies.Expires = DateTime.Now.AddDays(1);
                        Response.Cookies.Add(loginCookies);

                    }
                    Session[GUIConstants.CUSTOMERID_SESSION] = c.CustomerId;
                    Session[GUIConstants.LOGIN_SESSION] = c;
                    if (Session[GUIConstants.TARGET_URL] == null)
                    {
                        Response.Redirect("~/BackendPage/NewsPage.aspx");
                    }
                    else
                    {
                        Response.Redirect(Session[GUIConstants.TARGET_URL].ToString());
                    }
                }
                else
                {
                    ok = false;
                }
            }
            if (!ok)
            {
                lblError.Text = "Loi login";
            }
        }

        private void LoginFromCookies()
        {
            if (Request.Cookies[GUIConstants.LOGIN_COOKIES] != null)
            {
                HttpCookie loginCookies = Request.Cookies[GUIConstants.LOGIN_COOKIES];
                string loginName = loginCookies[GUIConstants.LOGIN_COOKIES_USERNAME];
                string password = loginCookies[GUIConstants.LOGIN_COOKIES_PASSWORD];
                List<Customer> lstCustomer = DataBase.GetAllCustomer();
                foreach (Customer c in lstCustomer)
                {
                    if (c.LoginName.CompareTo(loginName) == 0 && c.Password.CompareTo(password) == 0)
                    {
                        Session[GUIConstants.CUSTOMERID_SESSION] = c.CustomerId;
                        Session[GUIConstants.LOGIN_SESSION] = c;
                        Response.Redirect("~/BackendPage/NewsPage.aspx");
                    }
                }
            }
        }
    }
}
